{% extends "base.html" %}

{% block content %}
{% from "_macros.html" import render_tag %}

<section class="content-header">
    <h1>Last CVE updates</h1>
    <ol class="breadcrumb">
        <li><a href="{{ url_for('main.home') }}">OpenCVE</a></li>
        <li class="active">Dashboard</li>
    </ol>
</section>

<section class="content">
    <div class="row">
        <div class="col-md-8">
            {% if not changes %}
            <p class="alert alert-info">
                {% if not current_user.vendors and not current_user.products %}
                You have to subscribe to <a href="{{ url_for('main.vendors') }}">products and vendors</a> to see their last changes.
                {% else %}
                No changes available.
                {% endif %}
            </p>
            {% else %}
                <ul class="timeline">

                    {% set ns = namespace(last_day=false) %}
                    {% for change in changes %}
                    {% set ns.current_day = change.created_at.strftime("%d %b %Y") %}
                    <li class="time-label">
                        {% if ns.last_day != ns.current_day %}
                        <span class="bg-blue bg-opencve">
                            {{ ns.current_day }}
                        </span>
                        {% endif %}
                    </li>

                    {% if (change.events|length == 1) and (change.events[0].type.code == 'new_cve') %}
                    {% set new = true %}
                    {% else %}
                    {% set new = false %}
                    {% endif %}
                    <li>
                        {% if new %}
                        <i class="fa fa-plus bg-red"></i>
                        {% else %}
                        <i class="fa fa-edit bg-blue"></i>
                        {% endif %}
                        <div class="timeline-item">
                            <span class="time" title="{{ change.created_at.strftime('%d %b %Y, %H:%M') }}"><i class="fa fa-clock-o"></i> {{ change.created_at.strftime("%H:%M") }}</span>
                            <h3 class="timeline-header"><a href="{{ url_for('main.cve', cve_id=change.cve.cve_id) }}">{{ change.cve.cve_id }}</a> {% if new %}is a new CVE{% else %}has changed{% endif %}</h3>

                            <div class="timeline-body">
                                <div class="row">
                                    <div class="col-md-9">
                                        <p>{{ change.cve.summary }}</p>
                                    </div>
                                    <div class="col-sm-3">
                                        <table class="table">
                                            <tbody>
                                                <tr>
                                                    <td class="no-bordered">CVSS v3</td>
                                                    {% if change.cve.json.impact.baseMetricV3 %}
                                                    <td class="no-bordered"><span class="label {{ cvss_bg(change.cve.json.impact.baseMetricV3.cvssV3.baseScore)[1] }} label-cvss">{{ change.cve.json.impact.baseMetricV3.cvssV3.baseScore }} {{ change.cve.json.impact.baseMetricV3.cvssV3.baseSeverity }}</span></td>
                                                    {% else %}
                                                    <td>--</td>
                                                    {% endif%}
                                                </tr>
                                                <tr>
                                                    <td>CVSS v2</td>
                                                    {% if change.cve.json.impact.baseMetricV2 %}
                                                    <td><span class="label {{ cvss_bg(change.cve.json.impact.baseMetricV2.cvssV2.baseScore)[1] }} label-cvss">{{ change.cve.json.impact.baseMetricV2.cvssV2.baseScore }} {{ change.cve.json.impact.baseMetricV2.severity }}</span></td>
                                                    {% else %}
                                                    <td>--</td>
                                                    {% endif%}
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                                {% if not new %}
                                <div class="box-group" id="{{ change.id }}">
                                    {% for event in change.events %}
                                    <div class="panel box box-changes">
                                        <div class="box-header collapsed pointer change-collapsed" data-toggle="collapse" data-parent="#{{ change.id }}" href="#{{ event.id }}" aria-expanded="false">
                                            <h4 class="box-title">{{ event.type.code|capitalize }} changed
                                            <span class="box-meta">{{ event.details|event_excerpt|safe }}<i class="fa fa-arrow-circle-o-down"></i><i class="fa fa-arrow-circle-o-up"></i></span>
                                            </h4>
                                        </div>
                                        <div id="{{ event.id }}" class="panel-collapse collapse" aria-expanded="false">
                                            <div class="box-body">
                                                <table class="table">
                                                    <thead>
                                                    <th>Values Removed</th>
                                                    <th>Values Added</th>
                                                    </thead>
                                                    <tbody>
                                                    {% set template = 'report/' + event.type.code + '_details.html' %}
                                                    {% set hide_type = true %}
                                                    {% include template %}
                                                    </tbody>
                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                    {% endfor %}

                                </div>
                                {% endif %}

                                <table class="table no-margin-bottom">
                                    <thead>
                                        <th>Vendors</th>
                                        <th>Products</th>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>{{ change.cve.vendors|vendors_excerpt|safe }}</td>
                                            <td>{{ change.cve.vendors|products_excerpt|safe }}</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </li>
                    {% set ns.last_day = ns.current_day %}
                    {% endfor %}
                </ul>
                <div class="center">
                    <ul class="pagination">
                        {% set prev = page - 1 %}
                        {% if prev %}
                        <li class="previous"><a href="{{ url_for('main.home', page=prev) }}">  « prev </a></li>
                        {% endif %}
                        <li class="next"><a href="{{ url_for('main.home', page=page+1) }}">next »</a></li>
                    </ul>
                </div>
            {% endif %}
        </div>

        <div class="col-md-4">

            {% if current_user.vendors or current_user.products %}
            <div class="box box-primary">
                <div class="box-header">
                    <div class="box-title">Subscriptions</div>
                    <div class="box-tools pull-right">
                        <a class="btn btn-box-tool" href="{{ url_for('main.subscriptions') }}" data-toggle="tooltip" data-container="body" data-placement="left" title="Manage your subscriptions"><i class="fa fa-gears"></i></a>
                    </div>
                </div>
                <div class="box-body box-subscriptions">
                    {% if current_user.vendors %}
                    <h5><strong>Vendors <span>({{ current_user.vendors|length }})</span></strong></h5>
                    {% for vendor in current_user.vendors %}
                    <a class="label label-primary" href="{{ url_for('main.cves', vendor=vendor.name) }}">{{ vendor.name
                        | humanize }}</a>
                    {% endfor %}

                    {% if current_user.products %}
                    <hr/>
                    {% endif %}
                    {% endif %}
                    {% if current_user.products %}
                    <h5><strong>Products <span>({{ current_user.products|length }})</span></strong></h5>
                    {% for product in current_user.products %}
                    <a class="label label-primary"
                       href="{{ url_for('main.cves', vendor=product.vendor.name, product=product.name) }}">{{
                        product.name | humanize }}</a>
                    {% endfor %}
                    {% endif %}
                </div>
            </div>
            {% endif %}

            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title">Tags</h3>
                    <div class="box-tools pull-right">
                        <a class="btn btn-box-tool" href="{{ url_for('main.tags') }}" data-toggle="tooltip" data-container="body" data-placement="left" title="Manage your tags"><i class="fa fa-gears"></i></a>
                    </div>
                </div>
                {% if current_user.tags %}
                <div class="box-body">
                    {% for tag in current_user.tags %}
                    <a
                        href="{{ url_for('main.cves', tag=tag.name) }}"
                        data-toggle="tooltip"
                        data-container="body"
                        title="{{ tag.description }}"
                    >{{ render_tag(tag) }}</a>
                    {% endfor %}
                </div>
                {% endif %}
            </div>

            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title">Last Reports</h3>
                    <div class="box-tools pull-right">
                        <a class="btn btn-box-tool" href="{{ url_for('main.reports') }}" data-toggle="tooltip" data-container="body" data-placement="left" title="See all your reports"><i class="fa fa-list-ul"></i></a>
                    </div>
                </div>
                <div class="box-body">
                    <table class="table">
                        <thead>
                        <tr><th class="col-md-2">Date</th>
                        <th>Vendors &amp; Products</th>
                        </tr></thead>

                        <tbody>
                        {% for report in reports %}
                        <tr>
                            <td><a href="{{ url_for('main.report', link=report.public_link) }}">{{ report.created_at.strftime("%d/%m/%y") }}</a></td>
                            <td>{{ report.details|report_excerpt|safe }}</td>
                        </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>

{% endblock %}